window.onload = function () {
  if(true){
    document.getElementById("topnav").innerHTML += '<li><a href="manage.html">管理</a></li>';
  }
  
  content = document.getElementById("content");
  if (price[0].id == "unknown" && price[0].name == "?") {
    content.innerHTML = '<span class="qtitle">暂未找到任何有关信息</span>';
  } else {
    var pre =
      '<table border="0" width="100%"><tr><td style="width:81%">名称</td><td style="width:9%" align="center">单价(￥)</td><td width="10%" align="center">数量</td></tr>';
    for (var i = 0; i < price.length; i++) {
      pre +=
        "<tr><td>" +
        price[i].name +
        '</td><td align="center">' +
        price[i].price +
        '</td><td><div width="300px" align="center">' +
        '<input style="width: 30px; height: 30px; border: 2px white" type="button" disabled="true" value="-" onclick="reduce(\'' +
        price[i].id +
        '\')" id="red' +
        price[i].id +
        '"/>' +
        '<input style="text-align: center; width: 30px; height: 25px" type="text" value="0" onblur="check(\'' +
        price[i].id +
        '\')" id="' +
        price[i].id +
        '"/>' +
        '<input style="width: 30px; height: 30px; border: 2px white;" type="button" value="+" onclick="add(\'' +
        price[i].id +
        '\')" id="add' +
        price[i].id +
        '"/></div></td></tr>';
    }
    pre += "</table>";
    content.innerHTML = pre;
  }
};
function reduce(obj) {
  var price = document.getElementById(obj).value;
  if (price == 1) {
    document.getElementById(obj).value = 0;
    document.getElementById("red" + obj).disabled = true;
  } else {
    document.getElementById(obj).value = parseInt(price) - 1;
    document.getElementById("red" + obj).disabled = false;
  }
  $(obj);
}
function add(obj) {
  var price = document.getElementById(obj).value;
  document.getElementById(obj).value = parseInt(price) + 1;
  document.getElementById("red" + obj).disabled = false;
  $(obj);
}

function check(obj) {
  var reg = /^[1-9]\d*$/;
  var price = document.getElementById(obj).value;
  if (reg.test(price)) {
    if (price > 0) {
      document.getElementById("red" + obj).disabled = false;
    } else {
      document.getElementById("red" + obj).disabled = true;
    }
  } else {
    document.getElementById(obj).value = 0;
  }
  $(obj);
}

function $(obj) {
  var all = [0, 0];
  for (var i = 0; i < price.length; i++) {
    if (price[i].id == obj) {
      price[i].choose = parseInt(document.getElementById(obj).value);
    }
    all[0] += price[i].choose;
    all[1] += price[i].price * price[i].choose;
  }
  document.getElementById("price").innerText = "预计需要支出 " + all[1] + " 元";
  document.getElementById("choosed").innerText = "已选 " + all[0] + " 项";
}
